package com.kkm.controller;

import com.kkm.pojo.PageBean;
import com.kkm.pojo.Singer;
import com.kkm.pojo.Song;
import com.kkm.service.Imp.SingerServiceImp;
import com.kkm.service.SingerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@WebServlet("/singer")
public class SingerServlet extends BaseServlet {
    private SingerService service=new SingerServiceImp();
    //展示用户
    public void showSinger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //下面这两句是为了在模糊查询时也能分页
//        req.removeAttribute("SingerMethod");
//        req.setAttribute("SingerMethod","showSinger");

        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=8;
        PageBean<Singer> pageBeanSinger=service.showSinger(currentPage,rows);
        req.setAttribute("Singer",pageBeanSinger);
        req.getRequestDispatcher("/showSinger.jsp").forward(req,resp);
    }


    //模糊查询用户
    public void checkSinger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //下面这两句是为了在模糊查询时也能分页
//        req.removeAttribute("SingerMethod");
//        req.setAttribute("SingerMethod","checkSinger");

        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取查询条件，封装成集合
        Map<String, String[]> condition = req.getParameterMap();
        HttpSession session=req.getSession();
        //session保存查询条件，以便模糊查询分页
//        session.removeAttribute("condition");
//        session.setAttribute("condition",condition);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        req.removeAttribute("currPageMax");
        PageBean<Singer> singerList=service.checkSinger(condition,currentPage,rows);
        if(singerList==null){
//            singerList=service.showSinger(currentPage,rows); //如果需要在查询不到时显示全部数据
            req.setAttribute("Singer",singerList);
//            req.getRequestDispatcher("/showSinger.jsp").forward(req,resp);
            resp.getWriter().write("<script language='javascript'>alert('查无此项！');" +
                    "window.location.href='/showSinger.jsp';</script>");
        }else {
            req.setAttribute("Singer",singerList);
            req.getRequestDispatcher("/showSinger.jsp").forward(req,resp);
        }

    }
   //删除歌手
    public void deleteSinger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid =req.getParameter("id");
        service.deleteSinger(sid);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Singer> pageBeanSinger=service.showSinger(currentPage,rows);
        req.setAttribute("Singer",pageBeanSinger);
        req.getRequestDispatcher("/showSinger.jsp").forward(req,resp);
    }

     //修改用户（第一步接收信息）
    public void singerById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取歌手信息
        String sid=req.getParameter("id");
        System.out.println(sid+"编号");
        Singer singer=service.singerById(sid);
        req.setAttribute("singer",singer);
        //获取歌手的全部歌曲
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=5;
        // req.removeAttribute("currPageMax");
        PageBean<Song> pageBeanMusic=service.checkMusicById(sid,currentPage,rows);
        System.out.println(pageBeanMusic);
        req.setAttribute("Music",pageBeanMusic);

        req.getRequestDispatcher("/modifySinger.jsp").forward(req,resp);
    }
    //修改歌手信息
    public void modifySinger(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String introduction = req.getParameter("introduction");
        String birth = req.getParameter("birth");
        String country = req.getParameter("country");
        String files=req.getParameter("file");
        System.out.println("路径："+files);
//        String testname = req.getParameter("testname");
        System.out.println("歌手："+name+" "+sex+" "+introduction+" "+birth+" "+country);
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=8;
        PageBean<Singer> pageBeanSinger=service.showSinger(currentPage,rows);
        req.setAttribute("Singer",pageBeanSinger);
        req.getRequestDispatcher("/showSinger.jsp").forward(req,resp);
    }
//    //删除音乐
//    public void deleteMusic(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String sid =req.getParameter("id");
//        service.deleteMusic(sid);
//        System.out.println("删除方法 11");
//        String id=req.getParameter("id");
//        req.getRequestDispatcher("/singer?method=singerById&currentPage=1&id="+id).forward(req,resp);
//    }
 }
